﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using Duoease.ContainerModel.Entity;
using SmartMD.DataModel;

namespace Duoease.ContainerModel.Serialization
{
    public class DocumentModel
    {
        /// <summary>
        /// 容器存储的XML信息
        /// </summary>  
        private XmlDocument _Xml = new XmlDocument();
        public XmlDocument Xml
        {
            get { return _Xml; }
            set
            {
                _Xml = value;
            }
        }

        /**
         * 功能：序列化
         * 
         * */
        public void Serialize()
        {

        }

        /**
         * 功能：反序列化
         * 
         * */
        public void Deserialize()
        {

        }
        /**
         * 功能：构造容器xml
         * 
         * */
        public void ConstructContainerXml(BaseModel model)
        {
            string tagName = model.TagName + model.ID;
            XmlElement node = null;
            XmlElement childNode = null;
            XmlNode root = _Xml.SelectSingleNode("Container");
            if (root == null)
            {
                //创建一个根节点（一级）
                node = _Xml.CreateElement("Container");
                root = (XmlNode)node;
                _Xml.AppendChild(root);
            }

            XmlElement element = null;
            XmlNode Cnode = _Xml.SelectSingleNode("Container");
            if (Cnode == null)
            {
                return;
            }
            foreach (XmlNode xn in Cnode)//遍历
            {
                if (xn.Name == tagName)
                {
                    element = (XmlElement)xn;
                    break;
                }
            }
            childNode = model.ConstructXml(_Xml, tagName, element);
            if (element != null)
            {
                root.RemoveChild(element as XmlNode);
            }
            root.AppendChild(childNode);
        }

        /**
        * 功能：构造xml
        * 参数：
        *     type ：FridgeModel/ShelfMdoel/BoxModel
        * 
        * */
        public void ConstructXml(BaseModel model, ModelStyle modeltype,String ctnNo)
        {
            string tagName = modeltype + model.ID;
            XmlElement node = null;
            XmlElement childNode = null;
            XmlNode root = _Xml.SelectSingleNode("Container");
            if (root == null)
            {
                //创建一个根节点（一级）
                node = _Xml.CreateElement("Container");
                node.SetAttribute("ContainerStyle", modeltype.ToString()); //容器类型
                node.SetAttribute("ContainerNum", ctnNo); //容器编号
                root = (XmlNode)node;
                _Xml.AppendChild(root);
            }

            XmlElement element = null;
            XmlNode Cnode = _Xml.SelectSingleNode("Container");
            if (Cnode == null)
            {
                return;
            }
            foreach (XmlNode xn in Cnode)//遍历
            {
                if (xn.Name == tagName)
                {
                    element = (XmlElement)xn;
                    break;
                }
            }
            childNode = model.ConstructXml(_Xml, tagName, element);
            if (element != null)
            {
                root.RemoveChild(element as XmlNode);
            }
            root.AppendChild(childNode);
        }


        /**
         * 功能：初始化容器容量
         * 
         * */
        public void InitModelPercent()
        {
            XmlNode root = this.Xml.SelectSingleNode("Container"); //_Xml.SelectSingleNode("Container");

            if (root == null)
            {
                return;
            }
            XmlElement element = null;
            XmlElement element1 = null;
            foreach (XmlNode xn in root)
            {
                if (xn.Name.Contains("BoxModel"))
                {
                    element = (XmlElement)xn;
                    //element.SetAttribute("SpecimenCount", (0).ToString());
                    int sum = Convert.ToInt32(element.GetAttribute("Row")) * Convert.ToInt32(element.GetAttribute("Column"));
                    element.SetAttribute("Sum", sum.ToString());
                }
            }
            foreach (XmlNode xn in root)
            {
                if (xn.Name.Contains("ShelfModel"))
                {
                    element = (XmlElement)xn;
                    //element.SetAttribute("SpecimenCount", (0).ToString());
                    int sum = 0;
                    foreach (XmlNode xn1 in root)
                    {
                        if (xn1.Name.Contains("BoxModel" + element.GetAttribute("ID")))
                        {
                            element1 = (XmlElement)xn1;
                            sum += Convert.ToInt32(element1.GetAttribute("Sum"));
                        }
                    }
                    element.SetAttribute("Sum", sum.ToString());
                }
            }
        }


        /**
         * 功能：重新生成XML
         * 
         * */
        public string GetModelXml(string fridgeName)
        {
            XmlNode root = _Xml.SelectSingleNode("Container");
            foreach (XmlNode childNode in root.ChildNodes)
            {
                string id = fridgeName + childNode.Attributes["ID"].Value.ToString();
                XmlElement xe = (XmlElement)childNode;
                xe.SetAttribute("ID", id);
            }
            return _Xml.ToString();

        }
    }
}
